package com.wsh.${package}.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsh.utils.PageUtils;
import com.wsh.vo.${package}.${className}Vo;
import com.wsh.dto.${package}.${className}Dto;
import com.wsh.mapper.${package}.${className}Dao;
import com.wsh.entity.${package}.${className}Entity;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import com.wsh.${package}.${className}Service;
import org.springframework.stereotype.Service;
import com.wsh.base.BaseVo;
<#if isFile>
import org.springframework.beans.factory.annotation.Autowired;
import com.wsh.sys.FileService;
</#if>
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
* @author ${author}
* @date ${date}
*/
@Slf4j
@Service
public class ${className}ServiceImpl extends ServiceImpl<${className}Dao, ${className}Entity> implements ${className}Service {

<#if isFile>
    @Autowired
    private FileService fileService;
</#if>
    @Override
    public IPage<${className}Entity> getList(${className}Dto ${changeClassName}Dto) {
        log.info(this.getClass().getName() + " ==>> getList() params={}", ${changeClassName}Dto);
        // 分页数据处理
        IPage<${className}Entity> pages = PageUtils.getPage(${changeClassName}Dto);

        <#if searchs??>
            <#list searchs as column>
        ${column.colType} ${column.colName} = ${changeClassName}Dto.get${column.colNameToCap}();
            </#list>
        </#if>


        // 分页查询处理
        IPage<${className}Entity> page = baseMapper.selectPage(pages, new QueryWrapper<${className}Entity>()
                .select(<#if searchs??><#list searchs as column>"${column.baseName}"<#if column_has_next>, </#if></#list></#if>)
        <#if searchs??>
        <#list searchs as column>
            <#if column.colType != 'String'>
                .${column.searchType}(${column.colName} != null, "${column.baseName}", ${column.colName})
            <#else>
                .${column.searchType}(StringUtils.isNotBlank(${column.colName}), "${column.baseName}", ${column.colName})
            </#if>
        </#list>
        </#if>
                );
        log.info(this.getClass().getName() + " ==>> getList() result={}", page);
        return page;
    }

    @Override
    public BaseVo<${className}Vo> queryVo(String formId) {
        log.info(this.getClass().getName() + " ==>> queryVo() params={}", formId);
        ${className}Vo ${changeClassName}vo = new ${className}Vo();

        ${className}Entity ${changeClassName}Entity = getById(formId);

        BeanUtils.copyProperties(${changeClassName}Entity, ${changeClassName}vo);
        <#if isFile>
        List<Map<String, Object>> fileLists = fileService.getListByFormId(formId);
        BaseVo<${className}Vo> vo = new BaseVo<>(${changeClassName}vo, fileLists);
        log.info(this.getClass().getName() + " ==>> queryVo() result={}", vo);
        return vo;
        </#if>

        <#if !isFile>
        BaseVo<${className}Vo> vo = new BaseVo<>(${changeClassName}vo);
        log.info(this.getClass().getName() + " ==>> queryVo() result={}", vo);
        return vo;
        </#if>
    }

    @Override
    public boolean save${className}(${className}Dto ${changeClassName}Dto) {
        ${className}Entity ${changeClassName}Entity = new ${className}Entity();

        BeanUtils.copyProperties(${changeClassName}Dto, ${changeClassName}Entity);

        if (${changeClassName}Entity.get${searchId}() != null) {
            log.info(this.getClass().getName() + " ==>> edit save${className}() params={}", ${changeClassName}Dto);
        } else {
            log.info(this.getClass().getName() + " ==>> edit save${className}() params={}", ${changeClassName}Dto);
        }

        boolean saveEntity = saveOrUpdate(${changeClassName}Entity);

        <#if isFile>
        boolean saveFile = fileService.uploadFormId(${changeClassName}Dto.getNewFileList(), ${changeClassName}Dto.getFileRemoveIds(), ${changeClassName}Entity.get${getId}());
        </#if>
        boolean isSuccessful = saveEntity <#if isFile>&& saveFile</#if>;
        log.info(this.getClass().getName() + " ==>> save${className}() result={}", isSuccessful);
        return isSuccessful;
    }
}
